package io.github.liuyuhao412.manager.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import io.github.liuyuhao412.manager.entity.User;
import io.github.liuyuhao412.manager.exception.codeException;
import io.github.liuyuhao412.manager.mapper.UserMapper;
import io.github.liuyuhao412.manager.request.CreateUserRequest;
import io.github.liuyuhao412.manager.request.LoginRequest;
import io.github.liuyuhao412.manager.request.UpdateUserRequest;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private PasswordEncoder passwordEncoder;

	// 登录
	public User login(LoginRequest request) {
		User user = userMapper.getUserByUsername(request.getUsername());
		if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) {
			throw new codeException("用户名或密码错误");
		}
		if (user.getStatus() != null && user.getStatus() == 0) {
			throw new codeException("用户已被禁用，请联系管理员");
		}

		return user;
	}

	// 查询用户列表
	public List<User> listUsers() {
		return userMapper.listUsers();
	}

	// 创建用户
	public User createUser(CreateUserRequest request) {
		if (request.getUsername() == null || request.getUsername().trim().isEmpty()) {
			throw new codeException("用户名不能为空");
		}
		Integer roleValue = request.getRole();
		if (roleValue == null) {
			throw new codeException("角色不能为空");
		}
		String roleStr;
		switch (roleValue) {
		case 0:
			roleStr = "admin";
			break;
		case 1:
			roleStr = "user";
			break;
		default:
			throw new codeException("角色不合法");
		}

		User user = new User();
		user.setUsername(request.getUsername());
		user.setPassword(passwordEncoder.encode("Xxx@123456"));
		user.setRole(roleStr);
		user.setStatus(1);

		int rows = userMapper.insertUser(user);
		if (rows == 0) {
			throw new codeException("用户创建失败");
		}

		return user;
	}

	// 更新用户
	public User updateUser(UpdateUserRequest request) {
		if (request.getId() == null || request.getId() <= 0) {
			throw new codeException("用户不存在");
		}
		if (request.getRole() == null) {
			throw new codeException("角色不能为空");
		}
		String roleStr;
		switch (request.getRole()) {
		case 0:
			roleStr = "admin";
			break;
		case 1:
			roleStr = "user";
			break;
		default:
			throw new codeException("角色不合法");
		}

		int statusValue = (request.getStatus() != null) ? request.getStatus() : 1;

		User user = new User();
		user.setId(request.getId());
		user.setUsername(request.getUsername());
		user.setRole(roleStr);
		user.setStatus(statusValue);

		int rows = userMapper.updateUser(user);
		if (rows == 0) {
			throw new codeException("用户更新失败");
		}

		return user;
	}

	// 删除用户
	public void deleteUser(int id) {
		if (id <= 0) {
			throw new codeException("用户ID无效");
		}

		int rows = userMapper.deleteUser(id);
		if (rows == 0) {
			throw new codeException("删除用户失败，用户不存在");
		}
	}

	// 根据ID查询用户
	public User getUserById(Integer id) {
		return userMapper.getUserById(id);
	}
}
